package com.idbk.chargestation.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;
//import com.idbk.chargestation.activity.pile.ActivityPointMsg;
import com.idbk.chargestation.bean.JsonHistory;
//import com.idbk.chargestation.bll.pile.PileVendors;

import java.util.List;

/**
 * 充电点采集历史适配器
 *
 * @author liangzw, lupc
 */

public class GatherHistoryAdapter extends BaseAdapter {

    private List<JsonHistory> mData;
    private LayoutInflater mInflater;
    private Context mContext;

    public GatherHistoryAdapter(Context context, List<JsonHistory> list) {
        mData = list;
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        ViewHolder holder;
        if (convertView == null) {
            v = mInflater.inflate(R.layout.item_collection_history, parent, false);
            holder = new ViewHolder();
            holder.mParent = v;
            holder.textTime = (TextView) v.findViewById(R.id.textview_time);
            holder.textAddress = (TextView) v.findViewById(R.id.textview_point_address);
            holder.textVendor = (TextView) v.findViewById(R.id.textview_point_vendor);
            holder.imageStatus = (ImageView) v.findViewById(R.id.imageview_status);
            v.setTag(holder);
        } else {
            v = convertView;
            holder = (ViewHolder) v.getTag();
        }

        bindView(position, holder);

        return v;
    }

    private void bindView(int position, ViewHolder holder) {
        final JsonHistory item = mData.get(position);

        holder.textTime.setText(item.time == null ? "2016-01-01 00:00:00" : item.time);

        String address = String.format(
                "%s%s%s",
                item.provinceName == null ? "" : item.provinceName,
                item.cityName == null ? "" : item.cityName,
                item.address == null ? "" : item.address);
        holder.textAddress.setText(address);
//        String vendor = "供应商：" + new PileVendors(mContext).getPileVendorNameById(item.operatorId);
//        holder.textVendor.setText(vendor);

        if (item.checkStatus == JsonHistory.CHECK_STATUS_FAILED) {
            //审核失败
            holder.imageStatus.setImageResource(R.drawable.ico_verify_fault);
        } else if (item.checkStatus == JsonHistory.CHECK_STATUS_SUCCESS) {
            //审核成功
            holder.imageStatus.setImageResource(R.drawable.ico_verify_success);
        } else if (item.checkStatus == JsonHistory.CHECK_STATUS_IN) {
            //审核中
            holder.imageStatus.setImageResource(R.drawable.ico_verify_in);
        }

        holder.mParent.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (item.checkStatus == JsonHistory.CHECK_STATUS_FAILED) {
                    //审核失败
//                    Intent intent = new Intent(mContext, ActivityPointMsg.class);
//                    intent.putExtra(ActivityPointMsg.KEY_MODEL, ActivityPointMsg.MODEL_MODIFY);
//                    intent.putExtra(JsonHistory.KEY, item);
//                    mContext.startActivity(intent);
                } else if (item.checkStatus == JsonHistory.CHECK_STATUS_SUCCESS) {
                    Snackbar.make(v, "审核通过后不能修改！", Snackbar.LENGTH_SHORT).show();
                } else if (item.checkStatus == JsonHistory.CHECK_STATUS_IN) {
                    //审核中
                    Snackbar.make(v, "审核中不能修改！", Snackbar.LENGTH_SHORT).show();
                }
            }
        });
    }

    private class ViewHolder {
        private View mParent;
        private TextView textTime, textAddress, textVendor;
        private ImageView imageStatus;
    }

}
